<?php
/*****************************************************
 * File name: VersionModel.php
 * Create date: 2019/03/28
 * Author: liuyue
 * Description: 版本数据库接口
 *****************************************************/

namespace Model;

use Lib;
use Lib\Paas\Login;

class SeasonModel
{

    private $db = null;
    private $login = null;

    public function __construct()
    {
        $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        //实例化Login类
        $this->login = new Login();
        $this->table= 'tbseason';
    }

    /**
     * 根据版本号获取版本状态
     * @param $versionNumber 版本号
     * @return array
     */
    public function getSeasonStatus($seasonNumber)
    {
        $res = $this->db->table($this->table)->where(['seasonNumber' => $seasonNumber])->find('status');
        return $res;
    }

    /**
     * 更新版本号状态
     * @param $versionId 版本号Id
     * @param $new 新状态
     * @return bool
     */
    public function editSeasonStatus($versionId, $new)
    {


        $res = $this->db->table($this->table)->where(['id' => $versionId])->update([
            'status' => $new,
            'updateTime' => date('Y-m-d H:i:s', time()),
            'updateMan' => $this->login->getLoginInfo()['name']
        ]);
        logWrite(2, $res, 'tbversion');
        return $res;
    }

    /**
     * 更新版本信息
     * @param $data
     * @return mixed
     */
    public function editSeason($data)
    {
        try {
            $this->db->begin();
            $this->db->table($this->table)->where(['id' => $data['id']])->update([
                'isCurrent' => 1,
                'updateTime' => date('Y-m-d H:i:s', time()),
                'updateMan' => $this->login->getLoginInfo()['name']
            ]);
            $this->db->table('tbversion')->where(['id' => ['!=', $data['id']]])->update([
                'isCurrent' => 0,
                'updateTime' => date('Y-m-d H:i:s', time()),
                'updateMan' => $this->login->getLoginInfo()['name']
            ]);
            $this->db->commit();
            logWrite(2, true, $this->table);
            return true;
        } catch (\Exception $e) {
            $this->db->rollBack();
            logWrite(2, false, $this->table);
            return false;
        }
    }

    /**
     * 匹配版本
     * @param $versionNumber
     * @return bool
     */
    public function pregSeason($seasonNum)
    {
//        $isMatched = preg_match('/^[0-9\.]{6}$/', $seasonNum, $matches);
        $isMatched = preg_match('/^[0-9]{4}.S[0-9]{1}$/', $seasonNum, $matches);
        if ($isMatched) {
            return true;//是一个版本号
        }
        return false;
    }
        /**
     * 添加版本信息
     * @param $versionNumber
     * @return bool
     */
    public function addSeason($versionNumber)
    {
        //校验版本号
        $check = $this->pregSeason($versionNumber);
        if($check === false){
            $res['code'] = 1;
            $res['msg'] = "不是一个正确的版本号";
            return $res;
        }
        //检测版本号是否已经存在
        $find = $this->isExistsVersion($versionNumber);
        if($find === true){
            $res['code'] = 1;
            $res['msg'] = "版本号已经存在";
            return $res;
        }
        $save['versionNumber'] = $versionNumber;
        $save['createTime'] = date('Y-m-d H:i:s', time());
        $save['creater'] = $this->login->getLoginInfo()['name'];
        $find = $this->getCurrentVersion();
        //检测有无当前版本
        if(!$find){
            $save['isCurrent'] = 1;
        }
        $r = $this->db->table('tbversion')->insert($save);
        logWrite(1, $r, 'tbversion');
        $res['code'] = 0;
        $res['id'] = $r;
        return $res;
    }

    public function getSeasonByNum($seasonNum)
    {
        $where = ['seasonNumber' => $seasonNum];
        return $this->db->table('tbseason')->where($where)->find();
    }
    /**
     * 检测版本是否存在
     * @param $versionNumber
     * @return mixed
     */
    public function isExistsSeason($seasonNum)
    {
        $where = ['seasonNumber' => $seasonNum];
        $find = $this->db->table('tbseason')->where($where)->find();
        if ($find) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否有版本
     * @return bool
     */
    public function isExistVersionData()
    {
        $count = $this->db->table('tbversion')->count();
        if($count){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取当前版本
     * @return array
     */
    public function getCurrentSeason()
    {
        return $this->db->table($this->table)->where(['isCurrent' => 1])->find();
    }

    /**
     * 根据版本号Id添加到当前版本
     * @param $versionNumber
     * @return int
     */
    public function insertVersionReturnId($versionNumber){
        //检查版本号是否存在 存在直接返回 版本id
        $find = $this->db->table('tbversion')->where(['versionNumber' => $versionNumber])->find();
        if($find){
            return $find['id'];
        }

        $save['versionNumber'] = $versionNumber;
        $save['createTime'] = date('Y-m-d H:i:s', time());
        $save['creater'] = $this->login->getLoginInfo()['name'];
        $find = $this->getCurrentVersion();
        //检测有无当前版本
        if(!$find){
            $save['isCurrent'] = 1;
        }
        $res = $this->db->table('tbversion')->insert($save);
        logWrite(1, $res, 'tbversion');
        return $res;
    }

    /**
     * 获取可切换的版本号
     * @return array
     */
    public function getChangeSeasons()
    {
        return $this->db->table($this->table)->order('id','desc')->select();
    }

    /**
     * 获取最新的版本号
     * @return array
     */
    public function getLastVersion()
    {
        return $this->db->table('tbversion')->order('id')->find();
    }

}